<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * minat.php
 * @author ranuandra
 *
 * 10 Jul 2012 09:35:30
 *
 * 
 */

class Controller_Minat extends Kohana_Nasmoco
{
	
	public function action_index()
	{
		$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
		$minats = $pelamar->pelamarminat->find_all();
		$view = View::factory('minat/index');
		$view->datas = $minats;
		$this->template->container = $view;
	}
	
	public function action_add()
	{
		$bidangs = ORM::factory('mstbidang')->find_all()->as_array('id', 'bidang');
		$urut = array(1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5);
		$view = View::factory('minat/form');
		$view->bidangs = $bidangs;
		$view->urut = $urut;
		$this->template->container = $view;
	}
	
	public function action_save()
	{
		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();

			try 
			{
				$pelamar = ORM::factory('mstpelamar')->where('user_id', '=', Auth_ORM::instance()->get_user()->id)->find();
				if(isset($_POST['id']))
				{
					$minat = ORM::factory('pelamarminat', Request::current()->post('id'));
					$minat->mstbidang_id = Request::current()->post('mstbahasa_id');
					$minat->level = Request::current()->post('level');
					$minat->save();
				}
				else 
				{
					$minat = ORM::factory('pelamarminat');
					$minat->mstpelamar_id = $pelamar->id;
					$minat->created = date("Y-m-d H:i:s");
					$minat->create_data($_POST);
				}
				
				$db->commit();
				Request::current()->redirect(URL::base().'user');
				exit;
			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$errors = $e->errors('models');
				print_r($errors);
			}
		}		
	}
}